{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1797, 64), (1797,))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.load('digits/digits_images.npy')\n",
    "y = np.load('digits/digits_lable.npy')\n",
    "X.shape,y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16.0, 0.0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.max(),X.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ML.model_selection import train_test_split\n",
    "from ML.knn import KNeighborsClassifier\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, seed=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ML.knn.KNeighborsClassifier at 0x12fd35bd898>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf = KNeighborsClassifier(k=5, p=2)\n",
    "knn_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9832402234636871\n",
      "Wall time: 1.32 s\n"
     ]
    }
   ],
   "source": [
    "%time print(knn_clf.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "best_k 3\n",
      "best_p 4\n",
      "best_score 0.994413407821229\n",
      "5min 15s ± 2.35 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "best_score = 0\n",
    "best_k = 0\n",
    "best_p = 0\n",
    "for k in range(1, 21):\n",
    "    for p in range(1, 10):\n",
    "        knn_clf = KNeighborsClassifier(k=k, p=p)\n",
    "        knn_clf.fit(X_train, y_train)\n",
    "        score = knn_clf.score(X_test, y_test)\n",
    "        if score > best_score:\n",
    "            best_score = score\n",
    "            best_k = k\n",
    "            best_p = p\n",
    "            \n",
    "print('best_k', best_k)\n",
    "print('best_p', best_p)\n",
    "print('best_score', best_score)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
